package user.servlet;

import util.RLUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/RLUtilsServlet")
public class RLUtilsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String phone=request.getParameter("phone");
        String codeStr = String.valueOf((int) ((Math.random()*9+1)*1000));

        PrintWriter out=response.getWriter();
        if (RLUtils.sendSMS(phone,codeStr,"5")){
            HttpSession session=request.getSession();
            session.setAttribute("codeStr",codeStr);
            out.print(0);
        }else {
            out.print(1);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
